package com.shxt.lesson29.util;

import java.util.Map;
import java.util.List;

public class JSONParser {
    
    public static String MapToJSON(Map<String, Object> map){
        
        String[] properties = new String[map.size()];
        int i = 0;
        for(Map.Entry<String, Object> entry : map.entrySet()){
            properties[i++] = "\r\n    \"" + entry.getKey() + "\" : \"" + entry.getValue() + "\"";
        }
        
        String JSON = "{" + String.join(", ", properties) + " \r\n}";
        
    	return JSON;
    }
    
    public static String ListToJSON(List<Map<String, Object>> list){
        
        int i = 0;
        String[] objs = new String[list.size()];
        for(Map<String, Object> map : list){
            objs[i++] = "\r\n" + MapToJSON(map);
        }
        
        String JSON = "[" + String.join(", ", objs) + "\r\n]";
        return JSON;
    }
    
}


